<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
defined('BASEPATH') || exit('No direct script access allowed');
class Partner extends MY_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->verify_user();
	}

	public function index()
	{
		$this->view('partner');
	}

	public function data()
	{
		$result = $this->get_list('partner');
		echo json_encode($result);
	}

	public function add()
	{
		$this->load->view('partner_add');
	}

	public function insert()
	{
		$name = $this->input->post('name');
		$url = $this->input->post('url');
		$sort = $this->input->post('sort');
		$status = $this->input->post('status');
		$img = $this->input->post('img');
		$data = array('name' => $name, 'url' => $url, 'sort' => $sort, 'status' => $status, 'img' => $img, 'add_time' => time());
		$rs = $this->db->insert('partner', $data);

		if ($rs) {
			echo json_encode(array('msg' => '添加成功', 'error' => 0));
		}
		else {
			echo json_encode(array('msg' => '添加失败', 'error' => 1));
		}
	}

	public function edit($id)
	{
		$id = intval($id);
		$this->db->where('id', $id);
		$rs = $this->db->get('partner');
		$d = $rs->row_array();
		$this->load->view('partner_edit', $d);
	}

	public function update()
	{
		$name = $this->input->post('name');
		$url = $this->input->post('url');
		$sort = $this->input->post('sort');
		$status = $this->input->post('status');
		$img = $this->input->post('img');
		$data = array('name' => $name, 'url' => $url, 'sort' => $sort, 'status' => $status, 'img' => $img, 'add_time' => time());
		$id = intval($this->input->post('id'));
		$rs = $this->db->where('id', $id)->update('partner', $data);

		if ($rs) {
			echo json_encode(array('msg' => '修改成功', 'error' => 0));
		}
		else {
			echo json_encode(array('msg' => '修改失败', 'error' => 1));
		}
	}

	public function del()
	{
		$id = $this->input->get('id');

		if ($id) {
			$rs = $this->db->where_in('id', $id)->delete('partner');

			if ($rs) {
				echo json_encode(array('msg' => '删除成功', 'error' => 0));
			}
			else {
				echo json_encode(array('msg' => '删除失败', 'error' => 1));
			}
		}
		else {
			echo json_encode(array('msg' => '删除失败！至少选择一行数据', 'error' => 0));
		}
	}

	public function upload()
	{
		$config['upload_path'] = $_SERVER['DOCUMENT_ROOT'] . '/upload/partner/';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size'] = 5000;
		$config['file_name'] = rand(1000, 9999) . time();
		$this->load->library('upload', $config);

		if (!$this->upload->do_upload('Filedata')) {
			$data['error'] = 1;
			$data['msg'] = '上传失败';
		}
		else {
			$data['error'] = 0;
			$data['msg'] = '上传成功';
			$d = $this->upload->data();
			$data['file_name'] = $d['file_name'];
		}

		echo json_encode($data);
	}
}

?>
